#include "InetSocketAddress.h"
#include "TcpSocketOps.h"
#include <memory.h> // for memset

namespace dsth {
#ifdef IS_WINDOWS
InetSocketAddress::InetSocketAddress(unsigned short port) {
    memset(&addr_, 0x00, sizeof addr_);
    addr_.sin_family = AF_INET;
    addr_.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    addr_.sin_port = htons(port);
}

InetSocketAddress::InetSocketAddress(const char* server, unsigned short port) {
    //TODO: move to socket layer
    memset(&addr_, 0x00, sizeof addr_);
    addr_.sin_family = AF_INET;
    addr_.sin_port = htons(port);
    addr_.sin_addr.s_addr = ::inet_addr(server);
}
#else // POSIX
InetSocketAddress::InetSocketAddress(unsigned short port) {
    memset(&addr_, 0x00, sizeof addr_);
    addr_.sin_family = AF_INET;
    addr_.sin_addr.s_addr = htonl(INADDR_ANY);
    addr_.sin_port = htons(port);
}

InetSocketAddress::InetSocketAddress(const char* server, unsigned short port) {
    //TODO: move to socket layer
    memset(&addr_, 0x00, sizeof addr_);
    addr_.sin_family = AF_INET;
    addr_.sin_port = htons(port);
    addr_.sin_addr.s_addr = ::inet_addr(server);
}
#endif

std::string InetSocketAddress::ToHostPortFormat() const {
    char buf[32];
    net::ToHostPortFormat(buf, sizeof buf, addr_);
    return buf;
}

}
